package com.school.controller.admin;

import com.school.common.ServiceResultEnum;
import com.school.common.TeaShopCategoryLevelEnum;
import com.school.entity.Goods;
import com.school.entity.GoodsCategory;
import com.school.exception.MallException;
import com.school.service.GoodsCategoryService;
import com.school.service.GoodsService;
import com.school.util.PageQueryUtil;
import com.school.util.PageResult;
import com.school.util.Result;
import com.school.util.ResultGenerator;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Controller
@RequestMapping("/admin")
public class GoodsController {
    @Resource
    private GoodsService goodsService;
    @Resource
    private GoodsCategoryService goodsCategoryService;
    @GetMapping("/goods")
    public String goods(HttpServletRequest  request) {
        request.setAttribute("path", "tea_shop_goods");
        return "admin/tea_shop_goods";
    }

    @GetMapping("/goods/list")
    @ResponseBody
    public Result list(@RequestParam Map<String, Object> params) {
        if(StringUtils.isEmpty(params.get("page")) ||
                StringUtils.isEmpty(params.get("limit"))) {
            return ResultGenerator.genFailResult("参数异常！");
        }
        PageQueryUtil pageQueryUtil = new PageQueryUtil(params);
        PageResult pageResult = goodsService.getGoodsPage(pageQueryUtil);
        return ResultGenerator.genSuccessResult(pageResult);
    }
    @GetMapping("/goods/edit")
    public String edit(HttpServletRequest request) {
        List<GoodsCategory> firstLevelCategories  =goodsCategoryService.selectByLevelAndParentIdAndNumber(Collections.singletonList(0L), TeaShopCategoryLevelEnum.LEVEL_ONE.getLevel());
        if(!firstLevelCategories.isEmpty()){
            Long categoryId = firstLevelCategories.get(0).getCategoryId();
            List<GoodsCategory> secondLevelCategories =goodsCategoryService.selectByLevelAndParentIdAndNumber(Collections.singletonList(categoryId),TeaShopCategoryLevelEnum.LEVEL_TWO.getLevel());
            if(!secondLevelCategories.isEmpty()){
                Long categoryId1 = secondLevelCategories.get(0).getCategoryId();
                List<GoodsCategory> thirdLevelCategories =goodsCategoryService.selectByLevelAndParentIdAndNumber(Collections.singletonList(categoryId1),TeaShopCategoryLevelEnum.LEVEL_THREE.getLevel());
                request.setAttribute("thirdLevelCategories",thirdLevelCategories);
                request.setAttribute("secondLevelCategories",secondLevelCategories);
                request.setAttribute("firstLevelCategories",firstLevelCategories);
                request.setAttribute("path", "tea_shop_goods_edit");
                return "admin/tea_shop_goods_edit";
            }
        }
        MallException.fail("未查询到分类数据");
        return null;

    }
    @PostMapping("/goods/save")
    @ResponseBody
    public Result save(@RequestBody Goods goods ){
        if(StringUtils.isEmpty( goods.getGoodsName())||
                StringUtils.isEmpty(goods.getGoodsIntro())||
                StringUtils.isEmpty(goods.getGoodsCoverImg())) {
            return ResultGenerator.genFailResult("参数异常");
        }
        String result=goodsService.saveGoods(goods);
        if (ServiceResultEnum.SUCCESS.getResult().equals(result)){
            return ResultGenerator.genSuccessResult();
        }else{
            return ResultGenerator.genFailResult(result);
        }
    }
}


